//
//  ViewController.m
//  FirstTest
//
//  Created by iD Student on 7/23/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize Calculation;

- (void)viewDidLoad
{
    Calculation.text = [Calculation.text stringByAppendingFormat:@""];
    [super viewDidLoad];
    calcArray = [[NSMutableArray alloc]init];
    operators = [[NSMutableArray alloc]init];
    number = 0;
    
    /*[calcArray addObject:[NSNumber numberWithInt:1]];
    NSNumber* i = [calcArray objectAtIndex:0];
    int number = [i intValue]; 
    calcArray.count;*/
    
	
}

- (void)viewDidUnload
{
    [self setCalculation:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}



- (IBAction)numButtons:(id)sender 
{
    Calculation.text = [Calculation.text stringByAppendingFormat:[NSString stringWithFormat:@"%i",[sender tag]]];
    
    NSLog(@"%i",[sender tag]);
    
    number = number * 10 + [sender tag];
        
}

- (IBAction)functions:(id)sender 
{
    [calcArray addObject:[NSNumber numberWithInt: number]];
    number = 0;
    NSString *Symbol;
    switch ([sender tag]) {
        case 0:
            Symbol = @" + ";
            symbolNum = 0;
            break;
        case 1:
            Symbol = @" - ";
            symbolNum = 1;
            break;
        case 2:
            Symbol = @" * ";
            symbolNum = 2;
            break;
        case 3:
            Symbol = @" / ";
            symbolNum = 3;
            break;
        case 4:
            Symbol = @".";
            break;
        default:
            break;
    }
    
    [operators addObject:[NSNumber numberWithInt:symbolNum]];
    Calculation.text = [Calculation.text stringByAppendingFormat:[NSString stringWithString:Symbol]];
}

- (IBAction)equals:(id)sender 
{
    Calculation.text = [Calculation.text stringByAppendingFormat:[NSString stringWithString:@" = "]];
    for(int i = 0; i < operators.count; i++){
        if(i == 0){
            NSNumber* myFirstNum = [calcArray objectAtIndex: 0];
            total = myFirstNum.intValue;
        }
        else {
            
      
        NSNumber* j = [calcArray objectAtIndex:0];
        NSNumber* operator= [operators objectAtIndex:i] ;
        if(j.intValue+1<=calcArray.count-1)
        {
            NSNumber * secondNum  = [calcArray objectAtIndex:(i+1)];
        }
        int numbers = [j intValue]; 
        switch(operator.intValue){
            case 0:
                total += numbers;
                break;
            case 1:
                total -= numbers;
                break;
            case 2:
                total *= numbers;
                break;
            case 3:
                total /= numbers;
                break;
        }
    }
    Calculation.text = [Calculation.text stringByAppendingFormat:[NSString stringWithFormat:@"%f",total]];
    NSLog(@"%f",total);
        [calcArray removeAllObjects];
        [operators removeAllObjects];
    }
}

- (IBAction)clear:(id)sender 
{
    Calculation.text = @"";
    
}

@end
